// 加载分页
let laypage = layui.laypage;
let form = layui.form
let countTotal;
// 获取文章参数
let data = {
    pagenum: 1, // 第1页
    pagesize: 2 // 每页显示2条
}
// ---------------------- 获取文章列表 --------------------------
function renderArticle() {
    $.ajax({
        url: '/my/article/list',
        data: data,
        success: function (res) {
            // console.log(res);
            let arr = []
            res.data.forEach(item => {
                arr.push(`
                <tr>
                    <td>${item.title}</td>
                    <td>${item.cate_name}</td>
                    <td>${item.pub_date}</td>
                    <td>${item.state}</td>
                    <td>
                        <a href="../essayManage/editEssay.html?id=${item.id}" class="layui-btn layui-btn-xs">编辑</a>
                        <button type="button" data-id="${item.id}" class="layui-btn layui-btn-xs layui-btn-danger">删除</button>
                    </td>
                </tr>
                `)
            })
            $('tbody').html(arr.join(''))
            // 必须等到请求结束，获取 res.total ， 才能调用 showPage
            showPage(res.total)
            countTotal = res.total;
        }
    })
}
renderArticle()

// ---------------------- 分页 --------------------------
function showPage(x) {
    laypage.render({
        elem: 'page', //注意，不用加 # 号
        count: x, //数据总数，从服务端得到
        limit: data.pagesize, // 每页显示多少条
        curr: data.pagenum, // 当前页
        limits: [2, 3, 5, 10],
        layout: ['limit', 'prev', 'page', 'next', 'count', 'skip'], // 自定义排版
        jump: function (obj, first) {
            if (!first) {
                data.pagenum = obj.curr
                data.pagesize = obj.limit
                renderArticle()
            }
        }
    });
}

// ---------------------- 筛选 --------------------------
// 1.获取所有分类，渲染
$.ajax({
    url: '/my/category/list',
    success: function (res) {
        let arr = []
        res.data.forEach(item => {
            arr.push(`<option value="${item.id}">${item.name}</option>`)
        })
        $('select[name="category"]').append(arr.join(''))
        // 更新渲染
        form.render('select')
    }
})

// 2.完成筛选
$('#search').on('submit', function (e) {
    e.preventDefault()
    let state = $('select[name="state"]').val()
    let cate_id = $('select[name="category"]').val()
    if (state) {
        data.state = state
    } else {
        delete data.state
    }

    if (cate_id) {
        data.cate_id = cate_id
    } else {
        delete data.cate_id
    }

    data.pagenum = 1
    renderArticle()
})

// ---------------------- 删除 --------------------------
$('tbody').on('click', 'button:contains("删除")', function () {
    let id = $(this).data('id')
    layer.confirm('确定删除该项吗?', {
        icon: 3,
        title: '提示'
    }, function (index) {
        //do something
        $.ajax({
            url: '/my/article/delete/' + id,
            success: function (res) {
                // console.log(res);
                layer.msg(res.message)
                if (res.status === 0) {
                    renderArticle()
                    let totalFlag = countTotal - 1;
                    if ((totalFlag % data.pagesize) == 0) {
                        if (totalFlag <= 0) {
                            data.pagenum = 1;
                        } else {
                            data.pagenum = (totalFlag / data.pagesize);
                        }
                        renderArticle()
                    }
                }
            }
        })
        layer.close(index);
    });
})